<template>
	<view>
		<uni-section title="个人信息" type="line">
			<uni-card :title="currentUser.username" :sub-title="currentUser.account"
				thumbnail="https://img.yzcdn.cn/vant/cat.jpeg" @click="onClick">
				<text class="uni-body">这个家伙很懒，什么都没有留下。。。</text>
			</uni-card>
		</uni-section>
		<uni-section title="最近安排" type="line">
			<uni-card :title="afterOrder.hospitalName" v-if="afterOrder">
				<view class="uni-body">
					医院地址:{{afterOrder.address}}
				</view>
				<view>
					就诊信息:{{afterOrder.department1Name}} > {{afterOrder.department2Name}}
				</view>
				<view>
					就诊医生:{{afterOrder.doctorName}}
				</view>
				<view>
					就诊时间:{{afterOrder.year}}/{{afterOrder.month}}/{{afterOrder.day}}
					{{scheduleTime[afterOrder.scheduleCode]}}
				</view>
				<view>
					排号:{{afterOrder.rank}}
				</view>
			</uni-card>
			<uni-card title="暂无安排" v-else>

			</uni-card>
		</uni-section>
		<uni-section title="操作" type="line">


			<uni-list>
				<uni-list-item clickable title="订单列表" @click="handleOrderList" />
				<uni-list-item clickable title="修改个人信息" @click="updateInfo" />
			</uni-list>
		</uni-section>
	</view>
</template>

<script lang="ts" setup>
	import {
		getUserInfo
	} from '@/api/user';
	import {
		onShow
	} from '@dcloudio/uni-app';
	import {
		ref,
		unref
	} from 'vue';
	import {
		getCurrentUserOrder
	} from '@/api/orders'

	const currentUser = ref < any > ({})
	const orders = ref < any > ({})
	const afterOrder = ref < any > ({})
	const scheduleTime = ['全天', '上午', '下午', '晚上']
	onShow(() => {
		handleUser()
		handleOrder()

	})
	const handleUser = async () => {
		const {
			result
		} = await getUserInfo()
		currentUser.value = result
	}
	const handleOrder = async () => {
		const order = await getCurrentUserOrder()
		orders.value = order
		afterOrder.value = getAfterTowOrder()
	}
	const getAfterTowOrder = () => {
		const t = unref(orders)
		console.log('t', t);
		const afterOrder = t.filter(f => {
			const now = new Date();
			const year = now.getFullYear()
			const month = now.getMonth() + 1
			const day = now.getDay()
			return f.year >= year && f.month >= month && f.day >= day
		})
		return afterOrder[0]
	}
	const handleOrderList = () => {
		uni.navigateTo({
			url: `/pages/order/order?userId=${unref(currentUser).userId}`
		})
	}
	const updateInfo = () => {
		uni.navigateTo({
		url: '/pages/edit/edit'
		})
	}
</script>

<style>

</style>
